package week09;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


class ArrayListGeneralStack<E> implements GeneralStack<E>{
	private List<E> list=new ArrayList<>();
	
	public E push(E item) {
		if(item==null)
		return null;
		list.add(item);
		return item;
	}

	public E pop() {
		if(list.isEmpty())
			return null;
		return list.remove(list.size()-1);
	}

	public E peek() {
		if(list.isEmpty())
			return null;
		return list.get(list.size()-1);
	}

	public boolean empty() {
		return list.isEmpty();
	}

	public int size() {
		return list.size();
	}

	public String toString() {
		return list.toString();
	}
}
class Car{
	private int id;
	private String name;
	
	public Car(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String toString() {
		return "Car [id=" + id + ", name=" + name + "]";
	}
}
public class PTA5 {

	public static <E> void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String str=sc.nextLine();
		while(true) {
			if(str.equals("quit"))
				return;
			else if(str.equals("Integer")) {
				System.out.println("Integer Test");
				int m=sc.nextInt();
				int n=sc.nextInt();
				ArrayListGeneralStack<Integer> stack=new ArrayListGeneralStack<Integer>();
				for(int i=0;i<m;i++)
					System.out.println("push:"+stack.push((Integer)sc.nextInt()));
				if(n<m) {
				for(int j=0;j<n;j++)
					System.out.println("pop:"+stack.pop());
				System.out.println(stack.toString());
				int sum=0;
				for(int t=0;t<stack.size();t++) {
					Integer in=(Integer)stack.peek();
					sum+=in.intValue();
					stack.pop();
					t--;
				}
				System.out.println("sum="+sum);
				}
				else if(n>m) {
					for(int i=0;i<m;i++)
						System.out.println("pop:"+stack.pop());
					for(int i=m;i<n;i++)
						System.out.println("pop:null");
					System.out.println(stack);
					System.out.println("sum=0");
				}
				System.out.println(stack.getClass().getInterfaces()[0]);
			}
			else if(str.equals("Car")) {
				System.out.println("Car Test");
				int m=sc.nextInt();
				int n=sc.nextInt();
				ArrayListGeneralStack<Car> stack=new ArrayListGeneralStack<Car>();
				for(int i=0;i<m;i++)
					System.out.println("push:"+stack.push(new Car(sc.nextInt(), sc.next())));
				if(n<m) {
				for(int j=0;j<n;j++)
					System.out.println("pop:"+stack.pop());
				System.out.println(stack);
				for(int t=0;t<stack.size();t++) {
					Car car=(Car)stack.peek();
					System.out.println(car.getName());
					stack.pop();
					t--;
				}
				}
				else if(n>m) {
					for(int i=0;i<m;i++)
						System.out.println("pop:"+stack.pop());
					for(int i=m;i<n;i++)
						System.out.println("pop:null");
					System.out.println(stack);
				}
				System.out.println(stack.getClass().getInterfaces()[0]);
			}
			else if(str.equals("Double")) {
				System.out.println("Double Test");
				int m=sc.nextInt();
				int n=sc.nextInt();
				ArrayListGeneralStack<Double> stack=new ArrayListGeneralStack<Double>();
				for(int i=0;i<m;i++)
					System.out.println("push:"+stack.push(sc.nextDouble()));
				if(n<m) {
				for(int j=0;j<n;j++)
					System.out.println("pop:"+stack.pop());
				System.out.println(stack.toString());
				double sum=0.0;
				for(int t=0;t<stack.size();t++) {
					Double in=(Double)stack.peek();
					sum+=in;
					stack.pop();
					t--;
				}
				System.out.println("sum="+sum);
				}
				else if(n>m) {
					for(int i=0;i<m;i++)
						System.out.println("pop:"+stack.pop());
					for(int i=m;i<n;i++)
						System.out.println("pop:null");
					System.out.println(stack);
					System.out.println("sum=0");
				}
				System.out.println(stack.getClass().getInterfaces()[0]);
			}
			str=sc.nextLine();
		}
	}

}
